<?php

namespace app\admin\model\agent;

use think\Db;
use think\Model;


class Robotuserlist extends Model
{


    // 表名
    protected $table = 'robotuser_list';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;
    public function getParttimeStatusList()
    {
        return [ 'a1','a2' ];
    }

    //获取用户列表
    public function get_user_list()
    {
       $list = Db::query("select username from admin");

        $list = array_column($list,'username');

        return $list;
    }


    //爬虫机器人数
    public function get_spider_count($agentID='')
    {
        $count = 0 ;
        if($agentID){
            $list = Db::query("select * from robotuser_list where agentId = '".$agentID."'");

            $count = count($list);

        }
        return $count;
    }

    //今日、本周爬主播数
    //type 0今日 1本周
    public function get_spider_anchor_count($agentID='',$column='livestreamerNums',$type=0)
    {
        $count = 0 ;
        if($agentID){
            $s_date = date('Y-m-d',time());
            if($type){
                $week_day = date("w")?:7;
                $week_day  -= 1;
                $s_date = date('Y-m-d',strtotime("-{$week_day} day"));
            }
            $e_date = date('Y-m-d',strtotime("+1 day"));
            $list = Db::query("select * from robotuser_list where agentId = '".$agentID."'");
            foreach ($list as $k => &$v){
                 $sql = "SELECT * from spiderrobot_work_sum WHERE robotUser = '".$v['robotUser']."' and recordDate >= '".$s_date."' and recordDate < '".$e_date."'";
                 //my_debug(1,'$sql--54');
                 //my_debug(1,$sql);
                 $live_info = Db::query($sql);
                 if($live_info){
                     $count += array_sum(array_column($live_info,$column));
                 }
            }
        }
        return $count;

    }

    //验证机器人数
    //type 0今日 1本周
    public function get_verify_spider_count($type=0)
    {
        $count = 0 ;
        $s_date = date('Y-m-d',time());
        if($type){
            $week_day = date("w")?:7;
            $week_day  -= 1;
            $s_date = date('Y-m-d',strtotime("-{$week_day} day"));
        }
        $e_date = date('Y-m-d',strtotime("+1 day"));
        $sql = "SELECT * from livestreamer_list where spiderTime >= '".$s_date."' and spiderTime < '".$e_date."' group by spiderRobotUser";
        //my_debug(1,'$sql--79');
        //my_debug(1,$sql);
        $list = Db::query($sql);
        if(!empty($list)){
            $count = count($list);
        }
        return $count;
    }

    //验证机器人数
    //type 0今日 1本周 2本月
    public function get_verify_livestreamer_data($where='checkStatus != -1',$type=0)
    {

        $count = 0 ;
        $s_date = date('Y-m-d',time());
        if($type==1){
            $week_day = date("w")?:7;
            $week_day  -= 1;
            $s_date = date('Y-m-d',strtotime("-{$week_day} day"));
        }elseif($type==2){
            $s_date = date("Y-m-01", time());

        }
        $e_date = date('Y-m-d',strtotime("+1 day"));

        $where_sql = "where spiderTime >= '".$s_date."' and spiderTime < '".$e_date."'";
        if($where){
            if($where == 'signStatus = 1'){
                $where_sql .= " and signTime >= '".$s_date."' and signTime < '".$e_date."'";
                $where_sql .= ' and '.$where;
            }else if($where == 'success_radio'){
                $where_sql = "where spiderTime >= '".$s_date."' and spiderTime < '".$e_date."'";

            }else if($where == 'good_success_radio'){
                $where_sql .= " and signTime >= '".$s_date."' and signTime < '".$e_date."'";
                $where_sql .= ' and signStatus = 1';
                $where_sql .= ' and grade = 2';
            }else{
                $where_sql .= ' and '.$where;
            }



        }

        $sql = "SELECT * from livestreamer_list {$where_sql}";
        //my_debug(1,'$sql--121');
        //my_debug(1,$sql);
        $list = Db::query($sql);
        if(!empty($list)){
            $count = count($list);
        }
        return $count;
    }

    //经纪机器人数据
    //type 0今日 1本周
    public function get_agentrobot_work_data($where='',$type=0,$column='')
    {

        $count = 0 ;
        $s_date = date('Y-m-d',time());
        if($type){
            $week_day = date("w")?:7;
            $week_day  -= 1;
            $s_date = date('Y-m-d',strtotime("-{$week_day} day"));
        }
        $e_date = date('Y-m-d',strtotime("+1 day"));

        $where_sql = "where recordDate >= '".$s_date."' and recordDate < '".$e_date."'";
        if($where){
            $where_sql .= ' and '.$where;
        }

        $sql = "SELECT * from agentrobot_work_sum {$where_sql}";

        $list = Db::query($sql);
        if(!empty($list)){
            if($column == 'followNum'){
                $count += array_sum(array_column($list,$column));
            }else{
                $count = count($list);
            }

        }
        return $count;
    }

    //经纪机器人回关数据
    //type 0今日 1本周
    public function get_agentrobot_refollow_data($where='',$type=0,$column='')
    {

        $count = 0 ;
        $s_date = date('Y-m-d',time());
        if($type){
            $week_day = date("w")?:7;
            $week_day  -= 1;
            $s_date = date('Y-m-d',strtotime("-{$week_day} day"));
        }
        $e_date = date('Y-m-d',strtotime("+1 day"));

        $where_sql = "where refollowTime >= '".$s_date."' and refollowTime < '".$e_date."'";
        if($where){
            $where_sql .= ' and '.$where;
        }

        $sql = "SELECT * from agentrobot_hello_log {$where_sql}";

        $list = Db::query($sql);
        if(!empty($list)){
            if($column == 'followNum'){
                $count += array_sum(array_column($list,$column));
            }else{
                $count = count($list);
            }

        }
        return $count;
    }




}
